<?php
class shopListControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	
	public function onDefault(){
		if(SHOPLISTSHOW==1){
			C("shoplistarea")->onDefault();
			exit;
			//header("Location: /index.php?m=shoplistarea");
		} 
	 	$sc_id=M("site_city")->sc_id();
		/*
		if(!get('ajax')){
			if(!$sc_id){
				header("Location: /index.php?m=site_city");
			}else{
				
				$scarea=M("site_city")->selectRow("sc_id=".$sc_id);
				if($scarea['siteid']!=SITEID){
					header("Location: /index.php?m=site_city");
				}
			}
		}
		*/
		
		$limit=24;
		$start=get('per_page','i');
		$where=" status=2 AND siteid=".SITEID;
		$url="/index.php?m=shoplist";
		$catid=get('catid','i');
		if($catid){
			$cat=M("category")->selectRow("catid=".$catid);
			$catids=M("category")->id_family($catid);
			$where.=" AND catid in("._implode($catids).")";
			$url.="&catid=".$catid;
		}
		$keyword=get_post('keyword','h');
		if($keyword){
			$where.=" AND shopname like '%".$keyword."%'";
			$url.="&keyword=".urlencode($keyword);
		}
		$cityid=get('cityid','i');
		if($cityid){
			$where.=" AND cityid=".$cityid;
			$url.="&cityid=".$cityid;
		}
		
		
		 
		
		if($sc_id){
			$scarea=M("site_city")->selectRow("sc_id=".$sc_id);
			$scids=M("site_city")->id_family($sc_id);
			$where.=" AND sc_id in("._implode($scids).")";
			$url.="&sc_id=".$sc_id;
		}
		
		switch(get('filter')){
			case 'cspf':
					$where.=" AND cspf=1 ";
				break;
	
				break;
			case 'isnew':
					$where.=" AND isnew=1";
				break;
		}
		$url.="&filter=".get('filter','h');
		$ob=get('orderby','h');
		switch($ob){
			case 'distance':
					$orderby='grade DESC';
				break;
			case 'sold_num':
					$orderby='sold_num DESC';
				break;
			case 'raty_grade':
					$orderby='raty_grade DESC';
				break;
			default:
				$orderby='grade DESC';
			 	break;
		}
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>$orderby
		);
		$rscount=true;
		$data=M("shop")->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$gps=gps_get();
			$lat=$gps['lat'];
			$lng=$gps['lng'];
		if($data){
			foreach($data as $k=>$v){
				$distance[$k]=$v['distance']=distanceByLnglat($lng,$lat,$v['lng'],$v['lat']);
				$shopids[$k]=$v['shopid'];
				$data[$k]=$v;
			}
			if($ob=='distance'){
				array_multisort ( $distance ,  SORT_ASC ,  $shopids ,  SORT_ASC ,  $data );
			}
		}
		$per_page=$start+$limit;
		$per_page=$per_page>=$rscount?0:$per_page;
		$sclist=M("site_city")->children(0);
		$sdata=array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"per_page"=>$per_page,
			"scarea"=>$scarea,
			"sclist"=>$sclist,
			"cat"=>$cat,
			"catlist"=>M("category")->children(0,MODEL_SHOP_ID)
		); 
		 
		$this->smarty->goassign($sdata);	
	 
		$tpl=M("pagetpl")->get("shoplist","index","shoplist/index.html");
		$this->smarty->display($tpl);
	}
	/**
	*全文索引版
	*/
	public function onSphinx(){
		
	}
	
	public function onNear(){
		 
			 
			$gps=gps_get();
			$lat=$gps['lat'];
			$lng=$gps['lng'];
			$meter=0.00001*1.1;//1米以内
			$meter=isset($_GET['mi'])?$meter*intval($_GET['mi']):$meter*50000;
			$miarr=array();
		 
			   
			if($lat>0)
			{				
				$ilng=$lng+$meter;
				$mlng=$lng-$meter;
				$ilat=$lat+$meter;
				$mlat=$lat-$meter;
				$pagesize=20;
				$start=get('per_page','i');
				 
									
				$option=array(
					"where"=>"(lng<'$ilng' AND  lng>'$mlng' AND  lat>'$mlat' AND  lat<'$ilat')  AND status=2"
				);
				 					
				$arr=M('shop')->select($option);	
				if($arr){ 
					foreach($arr as $k=>$v){
						 $distance[$k]=$v['distance']=distanceByLnglat($lng,$lat,$v['lng'],$v['lat']);
						$shopids[$k]=$v['shopid'];
						 $arr[$k]=$v;
						 					
					}
					  
					array_multisort ( $distance ,  SORT_ASC ,  $shopids ,  SORT_ASC ,  $arr );

				}
				
				$this->smarty->assign("pagelist",$this->pagelist($rscount,$pagesize,"/index.php?m=near&a=index&mi={$_GET[mi]}"));
				
			}
			
			$this->smarty->assign(array(
				"list"=>$arr,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist
		));
			
			$this->smarty->display("shoplist/near.html");
	}
	
}

?>